package lapsnake.model.bounceback;

import lapsnake.main.LapSnake;
import lapsnake.util.Funky;

/**This class represents a substring which fills up a space between or surrounding a sequence of sibling nodes.
 * For example, consider the script: 
 * <pre>print "foo";</pre>
 * This will generate a Print node with a single child. The text of the child node is "foo". The two surrounding string nodes will then be "print " and ";"*/
public class StringNode implements ScriptNode
{
	public final String string;
	
	public StringNode(String value)
	{
		this.string = value;
	}
	
	@Override
	public boolean appendScript(StringBuilder sb, boolean startOfLine, int indentLevel)
	{
		if(startOfLine)
			sb.append(Funky.getIndentString(indentLevel*LapSnake.settings.indentSpaces.get()));
		sb.append(string);
		return (string.endsWith("\n"));
	}	
	
	@Override
	public String toString()
	{
		return getClass().getSimpleName()+"("+consoleString()+")";
	}

	private String consoleString()
	{
		String str = string.replace("\r", "\\"+'r');
		str = str.replace("\n", "\\"+'n');
		return str;
	}

	@Override
	public void visualizeScript(StringBuilder sb, int ident)
	{
		String idt = new String(new char[ident]).replaceAll("\0", "  ");
		sb.append(idt);
		sb.append('\'');
		sb.append(consoleString());
		sb.append('\'');
		sb.append('\n');
	}
}
